/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.calendar.weekview;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;

import mtt.calendar.eventeditor.EventEditorController;
import mtt.calendar.eventeditor.events.ShowEventEditorView;
import mtt.calendar.weekview.events.ShowWeekViewEvent;
import mtt.menu.events.ShowMainMenuEvent;
import mtt.rms.dataObjects.Event;
import mtt.rms.recordstores.calendar.Week;
import mtt.rms.recordstores.calendar.events.DeleteEventFromRmsEvent;
import mtt.rms.recordstores.calendar.events.GetCurrentWeekEvent;
import mtt.rms.recordstores.calendar.events.SetCurrenWeekEvent;
import hmvc.j2me.controller.AController;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.ShowDisplayAbleEvent;
import hmvc.j2me.events.TerminateEvent;

/**
 * 
 * 
 * @author jens.meiss
 */
public class WeekController extends AController implements CommandListener {

	private WeekView view;

	private WeekModel model;

	public WeekController() {

		setModel(new WeekModel());
		setView(new WeekView());

	}

	public void commandAction(Command c, Displayable d) {

		if (c == getView().getCommandBack()) {

			fireEventUp(new ShowMainMenuEvent());
			fireEvent(new TerminateEvent(), false, true, false);

		} else if (c == getView().getCommandAdd()) {

			addChildController((new EventEditorController()));

			fireEventDown(new ShowEventEditorView());

		} 
		
	}

	private void deleteEvent(Event event) {
		
		fireEventUp(new DeleteEventFromRmsEvent(event));
		fireEventUp(new GetCurrentWeekEvent(this));
		
	}

	private void editEvent(Event event) {
		
		addChildController(new EventEditorController(event));
		fireEventDown(new ShowEventEditorView());
		
	}

	private WeekModel getModel() {
		return model;
	}

	private WeekView getView() {
		return view;
	}

	public void init() {

		Form form = new Form("MTT - Wochenübersicht");
		form.setCommandListener(this);
		getView().setForm(form);
	
		Command commandBack = new Command("zurück", "zurück zum Hauptmenü",
				Command.BACK, 0);
		getView().setCommandBack(commandBack);

		Command commandEdit = new Command("Bearbeiten", "Eintrag bearbeiten",
				Command.ITEM, 1);
		getView().setCommandEdit(commandEdit);

		Command commandAdd = new Command("Hinzufügen", "Eintrag hinzufügen",
				Command.SCREEN, 1);
		getView().setCommandAdd(commandAdd);

		Command commandDelete = new Command("Löschen", "Eintrag Löschen",
				Command.ITEM, 1);
		getView().setCommandDelete(commandDelete);

		form.addCommand(commandBack);
		form.addCommand(commandAdd);

		register(SetCurrenWeekEvent.class);
		register(ShowWeekViewEvent.class);

	}

	public void receiveEvent(IEvent event) {

		if (event instanceof SetCurrenWeekEvent) {

			SetCurrenWeekEvent setCurrenWeekEvent = (SetCurrenWeekEvent) event;
			getModel().setWeekCurrent(setCurrenWeekEvent.getWeek());

			refreshView(setCurrenWeekEvent.getWeek());

		}
		if (event instanceof ShowWeekViewEvent) {

			fireEventUp(new GetCurrentWeekEvent(this));
			fireEventUp(new ShowDisplayAbleEvent(getView().getDisplayable()));

		}

	}

	private void refreshView(Week week) {

		getView().getForm().deleteAll();

		ItemCommandListener commandListener = new ItemCommandListener() {
			public void commandAction(Command arg0, Item arg1) {
				
				if (arg0 == getView().getCommandEdit()) {
					editEvent((Event) getModel().getHashtableItemIdEvent().get(arg1));
				}
				
				if (arg0 == getView().getCommandDelete()) {
					deleteEvent((Event) getModel().getHashtableItemIdEvent().get(arg1));
				}
				
			}
		};
		
		byte cnt = 0;
		while (cnt < 7) {

			String tag = null;
			switch (cnt) {
			case 0:
				tag = "Montag";
				break;
			case 1:
				tag = "Dienstag";
				break;
			case 2:
				tag = "Mittwoch";
				break;
			case 3:
				tag = "Donnerstag";
				break;
			case 4:
				tag = "Freitag";
				break;
			case 5:
				tag = "Samstag";
				break;
			case 6:
				tag = "Sonntag";
				break;
			}

			getView().getForm().append(new StringItem("Tag", tag));

			Vector vector = week.getVectorEvents(cnt);

			if (vector.size() == 0) {
				getView().getForm().append(
						new StringItem("Termine", "keine Termine vorhanden"));
			} else {
				
				if (getModel().getHashtableItemIdEvent() == null) {
					getModel().setHashtableItemIdEvent(new Hashtable());
				} else {
					getModel().getHashtableItemIdEvent().clear();
				}
				
				Enumeration enumeration = vector.elements();
				while (enumeration.hasMoreElements()) {
					Event event = (Event) enumeration.nextElement();
					
					StringItem item = new StringItem(event.getName(), event.getPlace());					
					
					item.addCommand(getView().getCommandEdit());
					item.addCommand(getView().getCommandDelete());
					item.setItemCommandListener(commandListener);
					
					getModel().getHashtableItemIdEvent().put(item, event);
					
					getView().getForm().append(item);
				}
			}
			cnt++;
		}
	}

	private void setModel(WeekModel model) {
		this.model = model;
	}

	private void setView(WeekView view) {
		this.view = view;
	}

	public void terminate() {
	}

}
